//
//  YHStatusToolBar.swift
//  YHWeibo
//
//  Created by 昱含 on 16/5/25.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit
import SnapKit

class YHStatusToolBar: UIView {
    
    var statusViewModel: YHStatusViewModel? {
        didSet{
            forwardBtn.setTitle(statusViewModel?.forwardText, forState: .Normal)
            commentBtn.setTitle(statusViewModel?.commentText, forState: .Normal)
            likeBtn.setTitle(statusViewModel?.likeText, forState: .Normal)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        backgroundColor = UIColor(white: 0.95, alpha: 1)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        setChildBtn(forwardBtn, title: "转发", imageName: "timeline_icon_retweet")
        setChildBtn(commentBtn, title: "评论", imageName: "timeline_icon_comment")
        setChildBtn(likeBtn, title: "赞", imageName: "timeline_icon_like")
        
        forwardBtn.snp_makeConstraints { (make) in
            make.left.top.bottom.equalTo(self)
        }
        
        commentBtn.snp_makeConstraints { (make) in
            make.left.equalTo(forwardBtn.snp_right)
            make.top.bottom.equalTo(self)
            make.width.equalTo(forwardBtn)
        }
        
        likeBtn.snp_makeConstraints { (make) in
            make.left.equalTo(commentBtn.snp_right)
            make.top.bottom.equalTo(self)
             make.right.equalTo(self)
            make.width.equalTo(forwardBtn)
        }
        
        let line1 = lineView()
        let line2 = lineView()
        let width = 1 / UIScreen.mainScreen().scale
        addSubview(line1)
        addSubview(line2)
        
        line1.snp_makeConstraints { (make) in
            make.left.equalTo(forwardBtn.snp_right)
            make.centerY.equalTo(self.snp_centerY)
            make.width.equalTo(width)
            make.height.equalTo(self.snp_height).multipliedBy(0.4)
        }
        
        line2.snp_makeConstraints { (make) in
            make.left.equalTo(commentBtn.snp_right)
            make.centerY.equalTo(self.snp_centerY)
            make.width.equalTo(width)
            make.height.equalTo(self.snp_height).multipliedBy(0.4)
        }
        
    }
    
    private func lineView() -> UIView {
        
        let line = UIView()
        line.backgroundColor = UIColor.lightGrayColor()
        return line
        
    }
    
    private func setChildBtn(btn: UIButton, title: String, imageName: String) {
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitle(title, forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(10)
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        btn.sizeToFit()
        self.addSubview(btn)
        
    }
    private lazy var forwardBtn: UIButton = UIButton()
    private lazy var commentBtn: UIButton = UIButton()
    private lazy var likeBtn: UIButton = UIButton()

}
